package com.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.CompletableFuture;

/**
 * Created by hdu_hyg on 2018/1/4.
 */
public class DateUtil {

    public static String getDateString(Date date) {
        return getDateString(date, "yyyy-MM-dd HH:mm:ss");
    }

    public static String getDateString(Date date, String dateFormat) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat);
        return simpleDateFormat.format(date);
    }

    public static Date getDate(String dateString) {

        Date date = null;
        try {
            date = getDate(dateString, "yyyy-MM-dd HH:mm:ss");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }

    public static Date getDate(String dateString, String dateFormat) {

        Date date = null;
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat);
            date = simpleDateFormat.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    // 获得当天凌晨的时间 00:00:00
    public static Date getDawn() {
//        Calendar cal = Calendar.getInstance();
//        cal.set(Calendar.HOUR_OF_DAY, 0);//24小时制
//        cal.set(Calendar.SECOND, 0);
//        cal.set(Calendar.MINUTE, 0);
//        cal.set(Calendar.MILLISECOND, 0);
//        Date dawn = cal.getTime();
        return getDawn(new Date());
    }

    // 获得date 凌晨的时间 00:00:00
    public static Date getDawn(Date date) {

        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, 0);//24小时制
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        Date dawn = cal.getTime();
        return dawn;
    }

    public static Date addMonth(Date date, int monthAmount) {
        return addInteger(date, Calendar.MONTH, monthAmount);
    }

    public static Date addDay(Date date, int dayAmount) {
        return addInteger(date, Calendar.DATE, dayAmount);
    }

    public static Date addHour(Date date, int hourAmount) {
        return addInteger(date, Calendar.HOUR_OF_DAY, hourAmount);
    }

    public static Date addMinute(Date date, int minuteAmount) {
        return addInteger(date, Calendar.MINUTE, minuteAmount);
    }

    public static Date addSecond(Date date, int secondAmount) {
        return addInteger(date, Calendar.SECOND, secondAmount);
    }

    private static Date addInteger(Date date, int dateType, int amount) {
        Date myDate = null;
        if (date != null) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(dateType, amount);
            myDate = calendar.getTime();
        }
        return myDate;
    }



    public static void sleep(long ms) { //// 会被 interrupt 打断线程的暂停状态
        try {
            Thread.sleep(ms);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void execTime(long ms) { // default 不清除 中断标志
        execTime(ms, false);
    }

    public static void execTime(long ms, boolean clearInterrupted) { // interrupt 可打断 park,wait,sleep,join

        long begin = System.currentTimeMillis();
        while (true) {

            if (clearInterrupted) {
                if (Thread.interrupted()) { //清除中断标记
                    break;
                }
            } else {
                if (Thread.currentThread().isInterrupted()) { //不清除中断标记
                    break;
                }
            }

            long end = System.currentTimeMillis();
            if (end - begin >= ms) {
                break;
            }
        }
    }

    public static <T> T getDelayValue(long ms, T value) {
        DateUtil.sleep(ms);
        return value;
    }

    public static <T> CompletableFuture<T> getFutureValue(long ms, T value) {

        return CompletableFuture.supplyAsync(() -> getDelayValue(1000, value));
    }

    public static void main(String[] args) {
        System.out.println(DateUtil.getDateString(DateUtil.getDawn(DateUtil.getDate("2017-12-10 10:12:50"))));

        LocalDate date = LocalDate.now();
        LocalTime time = LocalTime.now();
        LocalDateTime dateTime = LocalDateTime.now();

        Duration twoHours = Duration.ofHours(2);
        Duration twoDays = Duration.ofDays(2);

        LocalDate date1 = date.plus(2, ChronoUnit.DAYS); //ChronoUnit 时间单位
        date.plusDays(1);

        Period period = Period.between(date1, date);
        Period period1 = Period.ofDays(3);

        Instant instant = Instant.now();//相当于date



        System.out.println("Current date: " + date);
        System.out.println("Current date1: " + date1);
        System.out.println("Current time: " + time);
        System.out.println("Current dateTime: " + dateTime);
        System.out.println("duration twoDays: " + twoDays);
        System.out.println("period: " + period.get(ChronoUnit.DAYS));

        System.out.println(instant);


        int lengthOfMonth = date.lengthOfMonth();
        System.out.println(lengthOfMonth);
    }

    public static void test() {

        // Date -->(Instant) LocalDate
        Date date = new Date();
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();

        LocalDate localDate = instant.atZone(zoneId).toLocalDate();

        //LocalDate->ZonedDateTime->Instant->Date
        LocalDate localDate1 = LocalDate.now();
        ZonedDateTime zdt = localDate.atStartOfDay(zoneId);

        Date dat1e = Date.from(zdt.toInstant());



    }
}
